<template>
   <section>
        <!-- 搜索 -->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getUsers">查询</el-button>
				</el-form-item>
				<!-- <el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item> -->
			</el-form>
    </el-col>
    <el-table :data="carts" highlight-current-row v-loading="listLoading"
    :span-method="objectSpanMethod"
     @selection-change="selsChange" style="width: 100%;">
			<!-- <el-table-column type="selection" width="55">
			</el-table-column> -->
			<!-- <el-table-column type="index" width="60" label="用户id">
			</el-table-column> -->
            <!-- <el-table-column prop="u_img" label="头像" width="120" sortable>
			</el-table-column> -->
            <el-table-column prop="u_id" label="用户id" width="120" sortable>
			</el-table-column>
			<el-table-column prop="u_name" label="姓名" width="200" sortable>
			</el-table-column>
            <el-table-column prop="c_img" label="商品图" width="200" sortable>
                 <template   slot-scope="scope">            
                    <img :src="scope.row.c_img"  min-width="80" height="80" />
                 </template>         
			</el-table-column>
			<el-table-column prop="c_name" label="产品名" width="250" sortable>
			</el-table-column>
			<el-table-column prop="style" label="样式" width="200" sortable>
			</el-table-column>
            <el-table-column prop="c_num" label="数量" width="100" sortable>
			</el-table-column>
            <el-table-column prop="c_total" label="总金额" width="160" sortable>
			</el-table-column>
			<!-- <el-table-column label="操作" width="150">
				<template  scope="scope">
                    
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">
                        编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">
                        删除</el-button>
				</template>
			</el-table-column> -->
		</el-table>
        <el-col :span="24" class="toolbar">
			<!-- <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button> -->
			<el-pagination 
            layout="total, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange" 
            :page-size="pagesize"
            :current-page=1
            :total="total" 
            style="float:right;">
			</el-pagination>
		</el-col>
    </section>
</template>

<script>
export default {
data () {
    return {
          filters:{
            name:""
        },
        carts:[],
        page:1,
        listLoading:false,
        sels:[],
        pagesize:10,
        total:0,
        size:6,
        spanArr1:[],
        position1:""
    }
},
methods: {
    // 获取用户列表
    getUsers()
    {
        console.log(this.filters.name)
        
        let para = {
					page: this.page,
                    limit:this.pagesize,
                    name:this.filters.name
                };
                this.listLoading = true;
        this.$axios.post("http://127.0.0.1:8081/admin/cartmsg",para)
        .then(res=>
        {
            this.carts=res.data.data;
            this.total=res.data.count
            // console.log(this.total)
            this.listLoading = false;
            // console.log(res.data.data);
            for(let i=0;i<this.carts.length;i++)
            {
                this.carts[i].style="";
             
                for(let j in JSON.parse(this.carts[i].c_style))
                { 
                    //   console.log(JSON.parse(this.carts[i].c_style)[j]);
                    this.carts[i].style+=JSON.parse(this.carts[i].c_style)[j]+" "
                }
            }
             this.spanArr1= [];//在data里面定义
        this.position1 = 0; //在data里面定义
        this.carts.forEach((item,index) => {
          if( index === 0){
            this.spanArr1.push(1);
            this.position1 = 0;
            item.sequence=index+1;//设置序号
          }else{
            if(this.carts[index].u_id === this.carts[index-1].u_id ){
              this.spanArr1[this.position1] += 1;//连续有几行项目名名称相同
              this.spanArr1.push(0); // 名称相同后往数组里面加一项0
            //   console.log(this.spanArr)
              //当项目名称相同时，设置当前序号和前一个相同
              this.carts[index].sequence = this.carts[index-1].sequence;
            }else{
              this.spanArr1.push(1);
              this.position1 = index;
              //当项目名称不同时，将当前序号设置为前一个序号+1
              this.carts[index].sequence = this.carts[index-1].sequence+1;
            }
          }
        })
        })
       
        
    },
       objectSpanMethod({ row, column, rowIndex, columnIndex }) {
          if(columnIndex === 0||columnIndex === 1){  //序号列也进行合并(第一列)
          //this.spanArr这个数组里面存的是table里面连续的有几条数据相同
          //例如:[1,1,2,0,2,0]
          //1  代表的没有连续的相同的
          //2  代表连续的两个相同
          //0  代表是和上一条内容相同
          const _row = this.spanArr1[rowIndex];
          const _col = _row>0 ? 1 : 0;
        //   console.log("ppp")
        //   console.log(_row)
        //   console.log(_col)
          return {
            rowspan: _row, //行
            colspan: _col  //列
          }

        }
      },
    handleAdd()
    {
        console.log("添加");
    },
    selsChange(sels)
    {
        console.log(sels)
        this.sels=sels;
    },
    formatSex(row,column)
    {
        return row.u_sex == 2 ? '男' : row.u_sex == 1 ? '女' : '未知';
    },
    formatStatus(row,column)
    {
        return row.u_status == 1 ? '1' : row.u_status == 0 ? '0' : '未知';
    },
    handleEdit(index,row)
    {
        console.log("编辑");
    },
    handleDel(index,row)
    {
        let names=row.u_name;
        // console.log("删除");
        this.$confirm("确认删除选中记录吗？","提示",{
            type: 'warning'
        }).then(()=>
        {
            
            this.listLoading=true;
            let para={names:names};
            // para=names;
            this.$axios.post("http://127.0.0.1:8081/admin/deluser",para).then(res=>
            {
                this.listLoading=false;
                let ress=res.msg
                this.$message({
                    message:res.data.msg,
                    type:"success"
                })
                this.getUsers();
            })
           
        }).catch(()=>
        {

        })
    },
    batchRemove()
    {
        let names=this.sels.map(item => item.u_name).toString();
        this.$confirm("确认删除选中记录吗？","提示",{
            type: 'warning'
        }).then(()=>
        {
            
            this.listLoading=true;
            let para={names:names};
            // para=names;
            this.$axios.post("http://127.0.0.1:8081/admin/deluser",para).then(res=>
            {
                this.listLoading=false;
                let ress=res.msg
                this.$message({
                    message:res.data.msg,
                    type:"success"
                })
                this.getUsers();
            })
           
        }).catch(()=>
        {

        })
    },
    handleCurrentChange(val)
    {
        // console.log("val",val);
        this.page = val;
		this.getUsers();
    },
    handleSizeChange(val)
    {
        this.page = val;
		this.getUsers();
    }
    
},

mounted () {
    this.getUsers();
}
}
</script>

<style>

</style>
